package it.lapomuge.score.android.gui;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;
import java.util.UUID;

import it.lapomuge.score.android.ConnectedDevice;
import it.lapomuge.score.android.R;
import it.lapomuge.score.android.ScoreAndroidActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager.OnActivityResultListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class GUILogin {

	private ScoreAndroidActivity sa;
	private EditText login;
	private EditText password;
	private TextView errori;
	private BluetoothDevice[] devices;
	private Spinner comboDevice;
	private ConnectedDevice connectedDevice;

	public GUILogin(ScoreAndroidActivity sa) {
		this.sa = sa;
		sa.setContentView(R.layout.main);

		login = (EditText) sa.findViewById(R.id.jLogin);
		password = (EditText) sa.findViewById(R.id.jPwd);
		errori = (TextView) sa.findViewById(R.id.jErrori);

		final SharedPreferences myPrefs = sa.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);

		login.setText(myPrefs.getString("login", "andrea"));
		password.setText(myPrefs.getString("login", "muge10"));

		init();
	}

	private void init() {

		initDevice();
		initButton();

	}

	private void initButton() {
		Button b = (Button) sa.findViewById(R.id.jButtonLogin);

		b.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				BluetoothSocket socket = null;
				try {

					Spinner s = (Spinner) sa.findViewById(R.id.jServer);

					if (connectedDevice == null) {
						BluetoothDevice d = devices[s.getSelectedItemPosition()];
						socket = d.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
						socket.connect();
						errori.setText("Inizio...");
						connectedDevice = new ConnectedDevice(socket);
					}

					if (connectedDevice.login(login.getText().toString(), password.getText().toString())) {
						errori.setText("Login OK");
						new GUIAtleta(sa, connectedDevice);
					} else {
						errori.setText("Login Sbagliato");
					}

				} catch (IOException e) {
					if (socket != null) {
						try {
							socket.close();
						} catch (IOException e1) {

							e1.printStackTrace();
						}
					}
					errori.setText(e.getMessage());
					e.printStackTrace();
				}

			}

		});

	}

	private void initDevice() {

		BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (!mBluetoothAdapter.isEnabled()) {
			errori.setText("Non puoi utilizzare il programma senza bluetooth");
		} else {

			Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
			if (pairedDevices.size() > 0) {

				String[] deviceName = new String[pairedDevices.size()];
				devices = new BluetoothDevice[pairedDevices.size()];
				int i = 0;
				for (BluetoothDevice bluetoothDevice : pairedDevices) {
					devices[i] = bluetoothDevice;
					deviceName[i++] = bluetoothDevice.getName();
				}

				comboDevice = (Spinner) sa.findViewById(R.id.jServer);
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(sa, android.R.layout.simple_spinner_item, deviceName);
				comboDevice.setAdapter(adapter);

				mBluetoothAdapter.cancelDiscovery();

			} else {
				errori.setText("Associa un dispositivo al telefono");
			}
		}

	}

	public ConnectedDevice getConnectedDevice() {
		return this.connectedDevice;
	}

}
